package com.onehash.onebilling.gui;

import java.awt.*;

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.BorderFactory;
import java.awt.Color;
import javax.swing.border.TitledBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;



import com.onehash.onebilling.entity.*;

public class OneBillingLoginDlg extends JDialog{
    JPanel JPlogin = new JPanel();
    JLabel imageLabel = new JLabel();
    JTextField JTusername = new JTextField();
    JLabel JLusername = new JLabel();
    JLabel JLpassword = new JLabel();
    JButton Blogin = new JButton();
    JButton Bexit = new JButton();
    JPasswordField JPFpassword = new JPasswordField();
    JButton BchPassword = new JButton();

    /* Controller object */
    private UserRegister userRegister;
    private User loginUser;

    public OneBillingLoginDlg(Frame owner, String title, boolean modal) {
        super(owner, title, modal);
        try {
            this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            jbInit();
            pack();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    public OneBillingLoginDlg() {
        this(new Frame(), "", false);
        try {
            jbInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /* Setting Controller object */
    public void setControl(UserRegister userRegister){
        this.userRegister=userRegister;
    }
    
    public User getLoginUser() {
    	return loginUser;
    }


    private void jbInit() throws Exception {
        JPlogin.setLayout(null);
        imageLabel.setIcon(new ImageIcon(OneBillingLoginDlg.class.getResource("images/BgLogin.jpg")));
        imageLabel.setBounds(0, 0, 493, 314);
        JTusername.setBounds(new Rectangle(150, 107, 170, 22));
        JLusername.setFont(new java.awt.Font("Dialog", Font.BOLD, 12));
        JLusername.setForeground(new Color(0, 79, 138));
        JLusername.setText("Username:");
        JLusername.setBounds(new Rectangle(150, 84, 70, 14));
        JLpassword.setFont(new java.awt.Font("Dialog", Font.BOLD, 12));
        JLpassword.setForeground(new Color(0, 79, 138));
        JLpassword.setText("Password:");
        JLpassword.setBounds(new Rectangle(150, 136, 68, 14));
        Blogin.setBounds(new Rectangle(346, 184, 82, 28));
        Blogin.setFont(new java.awt.Font("Dialog", Font.BOLD, 12));
        Blogin.setForeground(new Color(0, 53, 125));
        Blogin.setMnemonic('L');
        Blogin.setText("Login");
        Blogin.addActionListener(new OneBillingLoginDlg_Blogin_actionAdapter(this));
        Bexit.setBounds(new Rectangle(319, 225, 109, 27));
        Bexit.setFont(new java.awt.Font("Dialog", Font.BOLD, 12));
        Bexit.setForeground(new Color(0, 53, 125));
        Bexit.setMnemonic('X');
        Bexit.setText("Exit");
        JPlogin.setMaximumSize(new Dimension(493, 314));
        JPlogin.setPreferredSize(new Dimension(493, 314));
        JPFpassword.setBounds(new Rectangle(150, 155, 170, 22));
        BchPassword.setBounds(new Rectangle(284, 267, 144, 27));
        BchPassword.setFont(new java.awt.Font("Dialog", Font.BOLD, 12));
        BchPassword.setForeground(new Color(0, 53, 125));
        BchPassword.setMnemonic('C');
        BchPassword.setText("Change Password");
        BchPassword.addActionListener(new
        		OneBillingLoginDlg_BchPassword_actionAdapter(this));
        Bexit.addActionListener(new OneBillingLoginDlg_Bexit_actionAdapter(this));
        this.setDefaultCloseOperation(javax.swing.WindowConstants.
                                      DISPOSE_ON_CLOSE);
        this.addWindowListener(new OneBillingLoginDlg_this_windowAdapter(this));
        getContentPane().add(JPlogin);
        JPlogin.add(Blogin);
        JPlogin.add(BchPassword);
        JPlogin.add(JPFpassword);
        JPlogin.add(JLusername);
        JPlogin.add(JTusername);
        JPlogin.add(JLpassword);
        JPlogin.add(Bexit);
        JPlogin.add(imageLabel);
        this.setSize(new Dimension( 493, 314));
        this.center();
    }

    private void center(){
        //center the dialog
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = this.getSize();
        if (frameSize.height > screenSize.height)
            frameSize.height = screenSize.height;
        if (frameSize.width > screenSize.width)
            frameSize.width = screenSize.width;
        this.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
    }

    public void Bexit_actionPerformed(ActionEvent e) {
        System.exit(0);
    }

    public void this_windowClosing(WindowEvent e) {
        System.exit(0);
    }

    public void Blogin_actionPerformed(ActionEvent e) {
        /* Check if the username and password is blank */
        if(JTusername.getText().equals("")||(JPFpassword.getPassword().length==0)){
            MessageDialog.showErrorDialog((Frame)this.getOwner(), this.getTitle(),
                                          "Username or Password must not be blank.");
        }else{ /* if username and password are entered */
            if (userRegister.checkUserNamePassword(JTusername.getText(),
                                         String.valueOf(JPFpassword.getPassword()))) {
            	loginUser = userRegister.getUser(JTusername.getText());
                this.dispose();
            } else {
                MessageDialog.showErrorDialog((Frame)this.getOwner(),
                                              this.getTitle(),
                                              "Incorrect username or password entered.");
                this.JTusername.setText("");
                this.JTusername.requestFocus();
                this.JPFpassword.setText("");
            }
        }
    }

    public void BchPassword_actionPerformed(ActionEvent e) {
        OneBillingChPassDlg chPassDlg=new OneBillingChPassDlg((Frame)this.getOwner(), this.getTitle()+" - Change Password", true);
        chPassDlg.setControl(this.userRegister);
        chPassDlg.setVisible(true);
    }
}


class OneBillingLoginDlg_BchPassword_actionAdapter implements ActionListener {
    private OneBillingLoginDlg adaptee;
    OneBillingLoginDlg_BchPassword_actionAdapter(OneBillingLoginDlg adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.BchPassword_actionPerformed(e);
    }
}


class OneBillingLoginDlg_this_windowAdapter extends WindowAdapter {
    private OneBillingLoginDlg adaptee;
    OneBillingLoginDlg_this_windowAdapter(OneBillingLoginDlg adaptee) {
        this.adaptee = adaptee;
    }

    public void windowClosing(WindowEvent e) {
        adaptee.this_windowClosing(e);
    }
}


class OneBillingLoginDlg_Blogin_actionAdapter implements ActionListener {
    private OneBillingLoginDlg adaptee;
    OneBillingLoginDlg_Blogin_actionAdapter(OneBillingLoginDlg adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.Blogin_actionPerformed(e);
    }
}


class OneBillingLoginDlg_Bexit_actionAdapter implements ActionListener {
    private OneBillingLoginDlg adaptee;
    OneBillingLoginDlg_Bexit_actionAdapter(OneBillingLoginDlg adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.Bexit_actionPerformed(e);
    }
}
